package com.example.textview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;
import java.util.Stack;

public class MainActivity extends AppCompatActivity {
    private Button b1, b2, b3;
    private CheckBox c1, c2, c3, c4;

    private EditText etUsername, etPassword;
    private Button bLogin;
    private Utils u = new Utils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event);

        etUsername = findViewById(R.id.editTextTextPersonName2);
        etPassword = findViewById(R.id.editTextTextPassword2);

        Map<String, String> userMap = u.getData(MainActivity.this);
        if(userMap != null){
            etUsername.setText(userMap.get("username"));
            etPassword.setText(userMap.get("password"));
        }

        bLogin = findViewById(R.id.buttonLogin);
        bLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (username == ""){
                    Toast.makeText(MainActivity.this, "input username", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password == ""){
                    Toast.makeText(MainActivity.this, "input password", Toast.LENGTH_SHORT).show();
                    return;
                }
                u.saveData(MainActivity.this, username, password);
                Toast.makeText(MainActivity.this, "saved", Toast.LENGTH_SHORT).show();
            }
        });

        b1 = findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
            }
        });

        b2 = findViewById(R.id.button2);
        b2.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(MainActivity.this, "longClick", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        b3 = findViewById(R.id.button3);
        b3.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Toast.makeText(MainActivity.this, "focuse "+hasFocus, Toast.LENGTH_SHORT).show();
            }
        });

        CheckBoxListener listener = new CheckBoxListener();

        c1 = findViewById(R.id.checkBox);
        c1.setOnCheckedChangeListener(listener);

        c2 = findViewById(R.id.checkBox2);
        c2.setOnCheckedChangeListener(listener);

        c3 = findViewById(R.id.checkBox3);
        c3.setOnCheckedChangeListener(listener);

        c4 = findViewById(R.id.checkBox4);
        c4.setOnCheckedChangeListener(listener);
    }

    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(MainActivity.this, buttonView.getText().toString()+isChecked, Toast.LENGTH_SHORT).show();
        }
    }
}